package com.newbee.activity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import com.hhu.newbee.R;
import com.newbee.adapter.MultChatResultListAdapter;
import com.newbee.listbean.MultResultBean;
import com.newbee.multchat.MultChatActivity;
import com.newbee.sound.Sound;
import com.newbee.sqlite.DBHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MultChatResultListActivity extends Activity implements
		OnClickListener, OnItemClickListener
{

	private ListView mListView;

	private DBHelper mDbHelper;

	private List<MultResultBean> mList;

	private TextView mEmptyView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		this.setContentView(R.layout.activity_multchatresult);
		mDbHelper = new DBHelper(this);
		initLayout();
		initDatas();
		super.onCreate(savedInstanceState);
	}

	private void initDatas()
	{

		mList = mDbHelper.selectMultResultFile();
		if (mList.size() == 0)
		{
			mListView.setEmptyView(mEmptyView);
		}
		else
		{
			mListView.setAdapter(new MultChatResultListAdapter(this, mList));
		}

	}

	private void initLayout()
	{
		this.findViewById(R.id.multchat_result_file_back).setOnClickListener(
				this);
		mListView = (ListView) this.findViewById(R.id.multresult_listview);
		mListView.setOnItemClickListener(this);
		mEmptyView = (TextView) this.findViewById(R.id.msglist_empty);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.multchat_result_file_back:
		{
			this.finish();

			break;
		}
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int posi, long arg3)
	{

		MultResultBean bean = mList.get(posi);
		File file = new File(Sound.DIR + bean.getFile());
		if (file.exists() && file.length() > 0)
		{
			String str;
			StringBuffer sb;
			try
			{
				sb = new StringBuffer();
				BufferedReader br = new BufferedReader(new FileReader(file));
				while (null != (str = br.readLine()))
				{
					sb.append(str);
				}
				br.close();
				str = sb.toString();

				Intent intent = new Intent(this, MultChatActivity.class);
				intent.putExtra(MultChatActivity.INIT_TAG, 1);
				intent.putExtra(MultChatActivity.MULT_RESULT, str);
				startActivity(intent);
			}
			catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		else
		{
			Toast.makeText(this, "文件异常", Toast.LENGTH_LONG).show();
		}

	}
}
